package com.example.module_clocking.viewModel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_clocking.model.ClockListEntity
import com.example.module_clocking.model.ClockTypeEntity
import com.example.module_clocking.model.ClockingBody
import com.example.module_clocking.model.ClockingEntity
import com.example.module_clocking.repo.ClockingRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class ClockingViewModel @Inject constructor(private val clockingRepo: ClockingRepo) :BaseViewModel(){
    val clockinglivedata by lazy {
        MutableLiveData<ClockingEntity>()
    }

    var clockingList = mutableListOf<ClockListEntity>()
    private val _mClockingList= MutableStateFlow<RequestEvent>(Loading)
    val clockingState get() = _mClockingList.asStateFlow()

    val ClockType = mutableListOf<ClockTypeEntity>()

    fun initList(){
        ClockType.add(ClockTypeEntity("全部"))
        ClockType.add(ClockTypeEntity("正常"))
        ClockType.add(ClockTypeEntity("迟到"))
        ClockType.add(ClockTypeEntity("早退"))
        ClockType.add(ClockTypeEntity("缺勤"))
    }
    fun getClockingList(id:Int){
        execute(
            action = clockingRepo.getClockingList(id),
            onError = {
                _mClockingList.value = Loading
            },
            onFinish = {
                clockingList.clear()
                clockingList.addAll(it.data)
                _mClockingList.value = Success
            }
        )
    }
    fun clocking(clockingBody: ClockingBody) {
        execute(
            action = clockingRepo.clocking(clockingBody),
            onError = {

            },
            onFinish = {
                clockinglivedata.value = it.data
                Log.e("Fan_qq", "${it.data}")
            }
        )
    }
}